package com.smasher.core.encrypt.symmetric;

import android.util.Base64;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


/**
 * DES 工具类
 */
public class DESUtil {
    private static final String ALGORITHM = "DES";
    private static final String CIPHER_TRANSFORMATION = "DES/CBC/PKCS5Padding";
    private static final int KEY_SIZE = 56; // DES 使用 56 位密钥

    /**
     * 生成 DES 密钥。
     *
     * @return Base64 编码的密钥字符串
     */
    public static String generateKey() {
        try {
            KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);
            keyGen.init(KEY_SIZE);
            SecretKey secretKey = keyGen.generateKey();
            return Base64.encodeToString(secretKey.getEncoded(), Base64.DEFAULT);
        } catch (Exception e) {
            throw new RuntimeException("Failed to generate DES key", e);
        }
    }

    /**
     * 生成随机的初始化向量 (IV)。
     *
     * @return Base64 编码的 IV 字符串
     */
    public static String generateIv() {
        byte[] iv = new byte[8]; // DES 需要 8 字节 IV
        new SecureRandom().nextBytes(iv);
        return Base64.encodeToString(iv, Base64.DEFAULT);
    }

    /**
     * 使用给定的密钥和 IV 对数据进行 DES 加密。
     *
     * @param data 数据字符串
     * @param key  Base64 编码的密钥字符串
     * @param iv   Base64 编码的 IV 字符串
     * @return Base64 编码的加密数据字符串
     */
    public static String encrypt(String data, String key, String iv) {
        try {
            Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
            SecretKeySpec keySpec = new SecretKeySpec(Base64.decode(key, Base64.DEFAULT), ALGORITHM);
            IvParameterSpec ivSpec = new IvParameterSpec(Base64.decode(iv, Base64.DEFAULT));
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);

            byte[] encryptedData = cipher.doFinal(data.getBytes());
            return Base64.encodeToString(encryptedData, Base64.DEFAULT);
        } catch (Exception e) {
            throw new RuntimeException("Encryption failed", e);
        }
    }

    /**
     * 使用给定的密钥和 IV 对数据进行 DES 解密。
     *
     * @param encryptedData Base64 编码的加密数据字符串
     * @param key           Base64 编码的密钥字符串
     * @param iv            Base64 编码的 IV 字符串
     * @return 解密后的数据字符串
     */
    public static String decrypt(String encryptedData, String key, String iv) {
        try {
            Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
            SecretKeySpec keySpec = new SecretKeySpec(Base64.decode(key, Base64.DEFAULT), ALGORITHM);
            IvParameterSpec ivSpec = new IvParameterSpec(Base64.decode(iv, Base64.DEFAULT));
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

            byte[] decodedData = Base64.decode(encryptedData, Base64.DEFAULT);
            byte[] decryptedData = cipher.doFinal(decodedData);
            return new String(decryptedData);
        } catch (Exception e) {
            throw new RuntimeException("Decryption failed", e);
        }
    }
}
